﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFontDatabase>
#include <QList>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initData();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initData() {
    mFontListModel = new QStandardItemModel();

    //获取字体
    QFontDatabase database;
    for(QString family: database.families()) {
        mFontListModel->appendRow(new QStandardItem(family) );
    }
    ui->fontListView->setModel(mFontListModel);
    QString sTemp = mFontListModel->index(0, 0).data().toString();
    ui->eidtFont->setText(sTemp);
    mFont.setFamily(sTemp);
    mStyleListModel =  new QStandardItemModel();
    for(QString style : database.styles(sTemp))  {
        mStyleListModel->appendRow(new QStandardItem(style) );
    }
    ui->styleListView->setModel(mStyleListModel);
    QString sTempStyle = mStyleListModel->index(0, 0).data().toString();
    ui->editStyle->setText(sTempStyle);
//    mFont.setStyle();
    mSizeListModel = new QStandardItemModel();
    for(int sz : database.pointSizes(sTemp, sTempStyle)) {
        mSizeListModel->appendRow(new QStandardItem(QString("%1").arg(sz)));
    }
    ui->sizeListView->setModel(mSizeListModel);
    int listCout = mSizeListModel->rowCount();
    if (listCout >0) {
       QString sTempSize = mSizeListModel->index(3,0).data().toString();
       ui->editSize->setText(sTempSize);
       mFont.setPointSize(sTempSize.toInt());
    }

   ui->lblSample->setFont(mFont);

}

void MainWindow::on_fontListView_clicked(const QModelIndex &index)
{
    QString strTemp = index.data().toString();
    ui->eidtFont->setText(strTemp);
    mFont.setFamily(strTemp);
    QFontDatabase database;
    mStyleListModel->clear();
    for(QString style : database.styles(strTemp))  {
        mStyleListModel->appendRow(new QStandardItem(style) );
    }
    mSizeListModel->clear();
    for(int sz : database.pointSizes(strTemp)) {
        mSizeListModel->appendRow(new QStandardItem(QString("%1").arg(sz)));
    }
    ui->editStyle->clear();
    ui->editSize->clear();
    ui->lblSample->setFont(mFont);
}

void MainWindow::on_styleListView_clicked(const QModelIndex &index)
{
    QString sFont = ui->eidtFont->text();
    QString strTemp = index.data().toString();
    ui->editStyle->setText(strTemp);
    if(strTemp == "Bold") {
        mFont.setBold(true);
    } else if(strTemp == "Italic") {
        mFont.setItalic(true);
    } else if(strTemp == "Bold Italic") {
         mFont.setBold(true);
        mFont.setItalic(true);
    } else {
        mFont.setBold(false);
       mFont.setItalic(false);
    }
    QFontDatabase database;

    mSizeListModel->clear();
    for(int sz : database.pointSizes(sFont, strTemp)) {
        mSizeListModel->appendRow(new QStandardItem(QString("%1").arg(sz)));
    }

    ui->editSize->clear();
    ui->lblSample->setFont(mFont);

}

void MainWindow::on_sizeListView_clicked(const QModelIndex &index)
{
      QString strTemp = index.data().toString();
      ui->editSize->setText(strTemp);
      mFont.setPointSize(strTemp.toUInt());
      ui->lblSample->setFont(mFont);
}



void MainWindow::on_chkStrikeout_clicked(bool checked)
{
    mFont.setStrikeOut(checked);
     ui->lblSample->setFont(mFont);
}

void MainWindow::on_chkUnderline_clicked(bool checked)
{
    mFont.setUnderline(checked);
     ui->lblSample->setFont(mFont);
}
